package com.example.service.impl;

import com.example.entity.Club;
import com.example.entity.ClubApplication;
import com.example.mapper.ClubMapper;
import com.example.service.ClubService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ClubServiceImpl implements ClubService {

    @Autowired
    private ClubMapper clubMapper;

    @Override
    public void add(Club club) {
        clubMapper.insert(club);
    }

    @Override
    public void updateById(Club club) {
        clubMapper.updateById(club);
    }

    @Override
    public void updateImageUrl(Club club) {
        clubMapper.updateImageUrl(club);
    }

    @Override
    public void deleteById(Integer id) {
        clubMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            deleteById(id);
        }
    }

    @Override
    public Club selectById(Integer id) {
        return clubMapper.selectById(id);
    }

    @Override
    public List<Club> selectAll(Club club) {
        return clubMapper.selectAll(club);
    }

    @Override
    public List<Club> selectList(Club club) {
        return null;
    }

    @Override
    public PageInfo<Club> selectPage(Club club, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Club> list = clubMapper.selectAll(club);
        return PageInfo.of(list);
    }

    @Override
    public List<Club> selectByPresidentId(Integer presidentId) {
        return clubMapper.selectByPresidentId(presidentId);
    }

    @Override
    public void joinClub(Integer clubId, Integer studentId) {
        // 检查是否已经申请过
        if (clubMapper.checkApplicationExists(clubId, studentId) > 0) {
            throw new RuntimeException("您已经申请过该社团");
        }
        // 检查是否已经是成员
        if (clubMapper.checkMemberExists(clubId, studentId) > 0) {
            throw new RuntimeException("您已经是该社团成员");
        }
        // 创建申请记录
        ClubApplication application = new ClubApplication();
        application.setClubId(clubId);
        application.setStudentId(studentId);
        application.setApplyTime(new Date());
        application.setStatus("待审核");
        clubMapper.createApplication(application);
    }

    @Override
    public boolean checkMember(Integer clubId, Integer studentId) {
        return clubMapper.checkMemberExists(clubId, studentId) > 0;
    }

    @Override
    public boolean checkApplication(Integer clubId, Integer studentId) {
        return clubMapper.checkApplicationExists(clubId, studentId) > 0;
    }

    @Override
    @Transactional
    public void quitClub(Integer clubId, Integer studentId) {
        // 检查是否是成员
        if (!checkMember(clubId, studentId)) {
            throw new RuntimeException("您不是该社团成员");
        }
        
        // 删除成员记录
        clubMapper.deleteMember(clubId, studentId);
    }

    @Override
    public List<Club> selectByStudentId(Integer studentId) {
        return clubMapper.selectByStudentId(studentId);
    }
} 